package com.example.remindme

import android.app.AppOpsManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.widget.Button
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import com.example.charactivity.ChartDaily.DayChartFragment
import com.example.charactivity.ChartWeekly.WeeklyFragment
import com.example.remindme.R


val PERMISSION_REQUEST_PACKAGE_USAGE_STATS=1101
val INTERVAL_DAYLY=10
val INTERVAL_WEEKLY=11
val INTERVAL_MOTHLY=12
val INTERVAL_YEARLY=13
class ChartActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chart)
// 权限判断
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP)
        {
            if(!hasPermisson()){
                startActivityForResult(
                    Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
                    PERMISSION_REQUEST_PACKAGE_USAGE_STATS
                )
            }
        }


            val fragment= DayChartFragment()
            supportFragmentManager.beginTransaction()
                .add(R.id.fragmentcontainer,fragment,"dayChart")
                .commit()


        val daily=findViewById<Button>(R.id.chartByday)
        val weekly=findViewById<Button>(R.id.chartByWeek)



        daily.setOnClickListener{
            val frag1= DayChartFragment()
            supportFragmentManager.beginTransaction()
                .replace(R.id.fragmentcontainer,frag1,"dayChart")
                .commit()
        }
        weekly.setOnClickListener{
         val  frag2=WeeklyFragment()
            supportFragmentManager.beginTransaction()
                .replace(R.id.fragmentcontainer,frag2,"weekChart")
                .commit()
        }





    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode== PERMISSION_REQUEST_PACKAGE_USAGE_STATS)
        {
            if(!hasPermisson())
            {
                startActivityForResult(
                    Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
                    PERMISSION_REQUEST_PACKAGE_USAGE_STATS
                )
            }
        }
    }
    private fun hasPermisson(): Boolean {
        var appOps:AppOpsManager= getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
        var code=0
        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.KITKAT){
            code=appOps.checkOpNoThrow(
                AppOpsManager.OPSTR_GET_USAGE_STATS,
                android.os.Process.myUid(),
                packageName
            )
        }
        return code==AppOpsManager.MODE_ALLOWED
    }



/*
   fun getDetailByHours()
    {
            for(i in 1..2)
            {
                val endTime:Long
                val beginTime=DateStatics.getDayStart(2*(i-1),0)
                if(i==12)
                endTime= DateStatics.getDayStart(0,1)
                else
                 endTime = DateStatics.getDayStart(i*2,0)

                Log.i("time","begin:$beginTime")
                Log.i("time","end:$endTime")
                val usageStateManager= this.getSystemService(USAGE_STATS_SERVICE) as UsageStatsManager
              //  val usageStatsList =usageStateManager .queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime) as List<UsageStats>
                // 得到每隔两个小时的数据

                val map=usageStateManager.queryAndAggregateUsageStats(beginTime,endTime) as Map<String,UsageStats>

// kotlin的for循环是闭区间！！！！！！！！！
                for(k in 0..4)  // cha12次，每次找对应的5个app
                {

                    Log.d("loop",arrayData[k]!!.name)
                    val temp:UsageStats = map.get(arrayData[k]!!.name)!!
                    usageStatics.printState(temp)
                    Log.d("loo","get")
                    aaChartOriginData[k][i-1]= UseTime(arrayData[k]!!.name,temp.totalTimeInForeground)
                }
            }
    }


 */

   // 创建了一个5行12列的数组

}